package com.thd.springboottest.camunda.controller;

import com.thd.springboottest.camunda.service.WorkFlowService;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * com.thd.springboottest.camunda.controller.CamundaController
 *
 * @author: wanglei62
 * @DATE: 2022/5/26 17:01
 **/
@Controller
@RequestMapping("/camunda/subprocess")
public class Camunda07subProcess {
    @Autowired
    private ProcessEngine processEngine;
    @Autowired
    private WorkFlowService workFlowService;
    /**
     * 部署
     */
    // url : http://127.0.0.1:8899/thd/camunda/subprocess/deploy
    @RequestMapping(value="/deploy",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> deploy(){
        String deployId = this.workFlowService.deploy("processes/07subProcess.bpmn");
        System.out.println("deployId:" + deployId);
        return ResponseEntity.ok("SUCCESS");
    }

    /**
     * 开启流程
     */
    // url : http://127.0.0.1:8899/thd/camunda/subprocess/startProcess/[businessKey:gateway001]
    @RequestMapping(value="/startProcess/{businessKey}",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> startProcess(@PathVariable String businessKey){
        ProcessInstance pi = processEngine.getRuntimeService().startProcessInstanceByKey("subProcess",businessKey);
       System.out.println("流程实例ID:" + pi.getProcessInstanceId());
        return ResponseEntity.ok("SUCCESS");
    }

    /*
    查询待办 http://127.0.0.1:8899/thd/camunda/queryTaskByBusinessKey/a01
    有taskA
     */
    /*
    设置流程变量
    http://127.0.0.1:8899/thd/camunda/addVariableForProcessInstance/[processInstanceId]
    {
        "approveList":[
            {"applier":"zhangsan1","approver":"lisi1"},
            {"applier":"zhangsan2","approver":"lisi2"},
            {"applier":"zhangsan3","approver":"lisi3"}
        ]
    }
     */

    /*
    完成taskA待办
    http://127.0.0.1:8899/thd/camunda/completeTask/27ebf361-e09b-11ec-a422-e86a64fc0114
     */

    /*
    查询待办
    有三个待办(apply), 因为进入了并行的子流程，代办人分别是zhangsan1,zhangsan2,zhangsan3
     */

    /*
    完成zhangsan1的代办
    http://127.0.0.1:8899/thd/camunda/completeTask/27ebf361-e09b-11ec-a422-e86a64fc0114
     */

    /*
    查询待办
    有三个待办，分别是zhangsan2,zhangsan3的apply代办和lisi1的approve代办（这个代办是由于zhangsan1完成后触发）
     */

     /*
    完成zhangsan2的代办
    http://127.0.0.1:8899/thd/camunda/completeTask/27ebf361-e09b-11ec-a422-e86a64fc0114
     */

 /*
    查询待办
    有三个待办，分别是zhangsan3的apply代办和lisi1、lisi2的approve代办（lisi2的代办是由于zhangsan2完成后触发）
     */

 /*
    完成zhangsan3的代办
    http://127.0.0.1:8899/thd/camunda/completeTask/27ebf361-e09b-11ec-a422-e86a64fc0114
     */

 /*
    查询待办
    有三个待办，分别是lisi3、lisi1、lisi2的approve代办（lisi3的代办是由于zhangsan3完成后触发）
     */

 /*
 分别完成lisi1,lisi2,lisi3的代办
 http://127.0.0.1:8899/thd/camunda/completeTask/1bcecbf4-e09c-11ec-a422-e86a64fc0114

  */
 /*
    查询待办
    有一个taskC的代办，并行子工作流已经结束
     */


}
